using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.Diagnostics;

namespace myBoard.Framework
{

    public partial class Flipper : System.Windows.Controls.UserControl
    {
        Storyboard _flipToBackSideStoryboard;
        Storyboard _flipToFrontSideStoryboard;
        public event EventHandler FlipToBackSideCompleted;
        public event EventHandler FlipToFrontSideCompleted;

        public Flipper()
        {
            InitializeComponent();
            _backSideContentGrid.Visibility = Visibility.Collapsed;
            _flipToBackSideStoryboard = this.FindResource("FlipToBackSide") as Storyboard;
            _flipToFrontSideStoryboard = this.FindResource("FlipToFrontSide") as Storyboard;

            if(_flipToBackSideStoryboard!=null)
                _flipToBackSideStoryboard.Completed += new EventHandler(_flipToBackSideStoryboard_Completed);

            if(_flipToFrontSideStoryboard!=null)
                _flipToFrontSideStoryboard.Completed += new EventHandler(_flipToFrontSideStoryboard_Completed);
        }

        ~Flipper()
        {
            
        }

        void _flipToBackSideStoryboard_Completed(object sender, EventArgs e)
        {
            if (FlipToBackSideCompleted != null)
                FlipToBackSideCompleted(this, e);
        }

        void _flipToFrontSideStoryboard_Completed(object sender, EventArgs e)
        {
            if (FlipToFrontSideCompleted != null)
                FlipToFrontSideCompleted(this, e);
        }

        public void SetFrontSideContent(FrameworkElement content)
        {
            double elementWidth = 0;
            double elementHeight = 0;

            if (double.IsNaN(content.ActualWidth) || content.ActualWidth == 0)
                elementWidth = content.Width;
            else
                elementWidth = content.ActualWidth;

            if (double.IsNaN(content.ActualHeight) || content.ActualHeight == 0)
                elementHeight = content.Height;
            else
                elementHeight = content.ActualHeight;

            this.Height = elementHeight;
            this.Width = elementWidth; 
            _frontSideContentGrid.Children.Clear();
            Grid g = new Grid();
            g.Width = elementWidth;
            g.Height = elementHeight;
            g.Margin = content.Margin;
            g.HorizontalAlignment = content.HorizontalAlignment;
            g.VerticalAlignment = content.VerticalAlignment;
            ImageBrush ib = new ImageBrush( Tools.GetBitmapSourceFromFrameworkElement(content));
            //VisualBrush vb = new VisualBrush(content);
            g.Background = ib;
            _frontSideContentGrid.Children.Add(g);            
        }

        public Grid FrontSideContentGrid
        {
            get
            {
                return _frontSideContentGrid;
            }
        }

        public void SetBackSideContent(FrameworkElement content)
        {
            this.Height = content.ActualHeight;
            this.Width = content.ActualWidth; 
            _backSideContentGrid.Children.Clear();
            Grid g = new Grid();
            g.Width = content.ActualWidth;
            g.Height = content.ActualHeight;
            g.Margin = content.Margin;
            g.HorizontalAlignment = content.HorizontalAlignment;
            g.VerticalAlignment = content.VerticalAlignment;
            ImageBrush ib = new ImageBrush(Tools.GetBitmapSourceFromFrameworkElement(content));
            //VisualBrush vb = new VisualBrush(content);
            g.Background = ib;
            _backSideContentGrid.Children.Add(g);
        }

        public void Rotate()
        {
            _frontSideRotatingControl.EulerAngles = new Thickness(30, 0, 0, 0);
        }

        public void ActiveViewIn3D()
        {
            _frontSideRotatingControl.ViewIn3D = true;
        }

        public void FlipToBackSide()
        {
            if (_flipToBackSideStoryboard != null)
                this.BeginStoryboard(_flipToBackSideStoryboard);
        }

        public void FlipToFrontSide()
        {
            if (_flipToFrontSideStoryboard != null)
                this.BeginStoryboard(_flipToFrontSideStoryboard);
        }
    }
}